package com.qiusuo0226.self.controller;

import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.core.util.UserInfoUtil;
import com.qiusuo0226.self.service.BillCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 账单类型 前端控制层
 * @author qiusuo
 * @date 2023/8/6 0:02
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/manager/billCategory")
public class BillCategoryController {

    @Autowired
    private BillCategoryService billCategoryService;

    /**
     * 新增账单类型
     * @param creater 账单类型创建者唯一标识
     * @param billType 记账类型，0:支出;1:收入
     * @param billCategory 账单类别
     * @return
     */
    @PostMapping("/add")
    public JsonResultModel add(HttpServletRequest request,
                               @NotBlank(message = "账单类型创建者唯一标识不能为空") String creater,
                               @NotBlank(message = "记账类型不能为空") String billType,
                               @NotNull(message = "账单类别不能为空") String billCategory) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billCategoryService.add(creater, billType, billCategory);
        } else {
            return result;
        }
    }

    /**
     * 编辑账单类型
     * @param creater 账单类型创建者唯一标识
     * @param categoryIid 账单类型唯一标识
     * @param billCategory 账单类别
     * @return
     */
    @PostMapping("/edit")
    public JsonResultModel edit(HttpServletRequest request,
                                @NotBlank(message = "账单类型创建者唯一标识不能为空") String creater,
                                @NotBlank(message = "账单类型唯一标识不能为空") String categoryIid,
                                @NotNull(message = "账单类别不能为空") String billCategory) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billCategoryService.edit(creater, categoryIid, billCategory);
        } else {
            return result;
        }
    }

    /**
     * 账单类型列表
     * @param creater 账单类型创建者唯一标识
     * @param billType 记账类型，0:支出;1:收入
     * @return
     */
    @PostMapping("/list")
    public JsonResultModel list(HttpServletRequest request,
                                @NotBlank(message = "账单类型创建者唯一标识不能为空") String creater,
                                @NotBlank(message = "记账类型不能为空") String billType) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billCategoryService.list(creater, billType);
        } else {
            return result;
        }
    }

    /**
     * 账单类型删除
     * @param creater 账单类型创建者唯一标识
     * @param categoryIid 账单类型唯一标识，多个账单类型用英文逗号隔开
     * @return
     */
    @PostMapping("/remove")
    public JsonResultModel remove(HttpServletRequest request,
                                  @NotBlank(message = "账单类型创建者唯一标识不能为空") String creater,
                                  @NotBlank(message = "账单类型唯一标识不能为空") String categoryIid) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billCategoryService.remove(creater, categoryIid);
        } else {
            return result;
        }
    }
}
